package com.wyg.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wyg.bean.User;
import com.wyg.service.LoginBiz;
/**
 * Servlet Filter implementation class UserFilter
 */
public class UserFilter implements Filter {

	public UserFilter() {
		// TODO Auto-generated constructor stub
		System.out.println("userfilter");
	}
	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}


	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		LoginBiz loginBiz = new LoginBiz();
		Cookie[] cookies = ((HttpServletRequest) request).getCookies();
		User user = loginBiz.getUserByCookie(cookies);
		if(user.getUserName()!=null&&user.getUserName().equals("admin")){
			filter.doFilter(request, response);
		}else{
			response.setContentType("text/html;charset=utf-8");
			((HttpServletResponse) response).sendRedirect(request.getServletContext().getContextPath()+"/start.jsp");
		}
		

		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
